//
//  CYUITextField_Account.m
//  Edu_iOS
//
//  Created by yanyan on 2017/9/8.
//  Copyright © 2017年 yanyan. All rights reserved.
//

#import "ECUITextField_Account.h"
#import "NSAttributedString+Extension.h"
@interface ECUITextField_Account()
{
    ECUButton *rightBtn;
}
@property(nonatomic,strong)UIView *bottomView;
@end
@implementation ECUITextField_Account
-(instancetype)initWithFrame:(CGRect)frame placeholder:(NSString *)placeholder leftImage:(UIImage *)leftImage
{
    if (self = [super initWithFrame:frame]) {
        self.leftImageView.image = leftImage;
        [self setPlaceholder:placeholder];
    }
    return self;
}
- (ECUButton *)rightButton
{
    if (!rightBtn) {
        rightBtn = [ECUButton buttonWithType:UIButtonTypeCustom];
        rightBtn.frame = CGRectMake(CGRectGetMaxX(self.textField.frame),(CGRectGetHeight(self.bounds) - 30)/2 , 30, 30);
        [rightBtn addTarget:self action:@selector(clickDisplayTextFieldText:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:rightBtn];
    }
    return rightBtn;
}
- (void)setIsSecureTextEntry:(BOOL)isSecureTextEntry
{
    _isSecureTextEntry = isSecureTextEntry;
    self.textField.secureTextEntry = isSecureTextEntry;
}
-(void)addSubviews{
    
    [super addSubviews];
    _bottomView = [[UIView alloc]initWithFrame:self.bounds];
    _bottomView.backgroundColor = [UIColor clearColor];
    _bottomView.layer.borderWidth = 1 * kPercentage;
    _bottomView.layer.cornerRadius = 5 * kPercentage;
    _bottomView.layer.masksToBounds = YES;
    [self addSubview:_bottomView];
    self.separatorColor = [UIColor colorWithHexString:@"#e5e5e5"];
    //textfield 左边的view
    UIView *leftView = [[UIView alloc]init];
    leftView.frame = CGRectMake(0, 0, CGRectGetHeight(self.frame)-10, CGRectGetHeight(self.frame));
    _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, leftView.center.y - 10, 14, 20)];
    [leftView addSubview:self.leftImageView];
    
    //textfield
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 1, self.frame.size.width, self.frame.size.height - 2)];
    _textField.font = [UIFont systemFontOfSize:15 * kPercentage];
    self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.textField.delegate = self;
    self.textField.leftView = leftView;
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    self.textField.autocorrectionType = UITextAutocorrectionTypeNo;
    self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self addSubview:self.textField];

    
}
-(void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    _textField.attributedPlaceholder = [NSAttributedString attributedStringWithString:placeholder color:[UIColor colorWithHexString:@"#808080"] font:_textField.font];

}
- (void)setSeparatorColor:(UIColor *)separatorColor
{
    _separatorColor = separatorColor;
    _bottomView.layer.borderColor = _separatorColor.CGColor;
    
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
    
}
#pragma mark--键盘弹起的时候不遮挡 button
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (self.block_beginEditing) {
        self.block_beginEditing(self);
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.block_endEditing) {
        self.block_endEditing(self);
    }
    
}
-(void)clickDisplayTextFieldText:(UIButton *)button{
    
//    button.selected = !button.selected;
//    if (self.isSecureTextEntry) {
//        _textField.font = [UIFont systemFontOfSize:17];
//        if (button.selected) {
//            self.textField.secureTextEntry = NO;
//        } else {
//            self.textField.secureTextEntry = YES;
//        }
//        [self.textField becomeFirstResponder];
//    }
    if (self.block_rightButton) {
        self.block_rightButton(button);
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _bottomView.frame = self.bounds;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
